package yqlweb.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import yqlweb.modelo.Foto;
import yqlweb.modelo.Clima;
import yqlweb.modelo.PerfilTwitter;

public class Parse {

    public List<Foto> xmlToObjectFotos(InputStream xml) {

        try {
            Foto foto;
            List<Foto> listaFotos = new ArrayList<Foto>();
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml);
            Element query = document.getDocumentElement();
            Element results = (Element) query.getChildNodes().item(0);
            for (int e = 0; e < results.getChildNodes().getLength(); e++) {
                foto = new Foto();
                Element photos = (Element) results.getChildNodes().item(e);
                foto.setId(photos.getAttribute("id"));
                foto.setFarm(photos.getAttribute("farm"));
                foto.setSecret(photos.getAttribute("secret"));
                foto.setServer(photos.getAttribute("server"));
                foto.setTitulo(photos.getAttribute("title"));
                String url = "http://farm" + foto.getFarm() + ".static.flickr.com/" + foto.getServer() + "/" + foto.getId() + "_" + foto.getSecret() + ".jpg";
                foto.setUrl(url);
                listaFotos.add(foto);
            }

            return listaFotos;


        } catch (Exception ex) {
            Logger.getLogger(Parse.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    public Clima xmlToObjectClima(InputStream xml) {

        try {
            if (xml != null) {
                Clima clima = new Clima();
                Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml);

                String titulo = document.getElementsByTagName("title").item(0).getTextContent();
                if (!titulo.equals("City not found")) {
                    clima.setTitulo(titulo);
                    String descricao = document.getElementsByTagName("description").item(0).getTextContent();
                    clima.setDescricao(descricao);
                    Element condicao = (Element) document.getElementsByTagName("yweather:condition").item(0);
                    String data = condicao.getAttribute("date");
                    clima.setData(data);
                    String temperetura = condicao.getAttribute("temp");
                    clima.setTempo(temperetura);
                    String texto = condicao.getAttribute("text");
                    clima.setCondicao(texto);

                    return clima;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception ex) {
            Logger.getLogger(Parse.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    public PerfilTwitter xmlToObjectPerfilTwitter(InputStream xml) {

        try {
            PerfilTwitter perfil = new PerfilTwitter();
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml);

            String name = document.getElementsByTagName("name").item(0).getTextContent();
            perfil.setNome(name);
            String descricao = document.getElementsByTagName("description").item(0).getTextContent();
            perfil.setDescricao(descricao);
            String username = document.getElementsByTagName("screen_name").item(0).getTextContent();
            perfil.setUsername(username);
            String localizacao = document.getElementsByTagName("location").item(0).getTextContent();
            perfil.setLocalizacao(localizacao);
            String url_imagem = document.getElementsByTagName("profile_image_url").item(0).getTextContent();
            perfil.setUrl_imagem(url_imagem);
            String ultimo_tweet = document.getElementsByTagName("text").item(0).getTextContent();
            perfil.setUltimo_tweet(ultimo_tweet);
            String qtd_followers = document.getElementsByTagName("followers_count").item(0).getTextContent();
            perfil.setQtd_followers(Integer.parseInt(qtd_followers));


            return perfil;
        } catch (Exception ex) {
            Logger.getLogger(Parse.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

  
}
